package top.hg.tool.modules.car.article;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.hg.tool.common.util.Assert;
import top.hg.tool.common.util.R;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: skh
 * @Date: 2019/7/30 10:31
 * @Description:
 */
@RestController
@RequestMapping("/car")
@Api(value = "文章管理")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation("新增文章")
    @PostMapping("/saveArticle")
    public R saveArticle(@RequestBody ArticleEntity articleEntity) {
        Assert.validateEntity(articleEntity);
        Integer id = articleService.saveArticle(articleEntity);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("id", id);
        return R.success(map);
    }

    @ApiOperation("获取文章列表")
    @GetMapping("/getArticleList")
    public R getArticleList() {
        List<ArticleEntity> articleEntityList = articleService.getArticleList();
        return R.success(articleEntityList);
    }

    @ApiOperation("获取文章详情")
    @GetMapping("/getArticleById")
    public R getArticleById(Integer id) {
        Assert.isNull(id, "文章id不能为空");
        ArticleEntity articleEntity = articleService.getArticleById(id);
        return R.success(articleEntity);
    }
}
